package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.entity.Column;
import edu.bit.water.entity.MetaSensor;
import edu.bit.water.util.ColumnUtil;
import edu.bit.water.util.ListToSqlString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import edu.bit.water.service.EntitySensorService;
import edu.bit.water.service.MetaSensorService;
import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/entity_sensor")
public class EntitySensorController {

    @Autowired
    private EntitySensorService entitySensorService;
    @Autowired
    private MetaSensorService metaSensorService;

    /**
     * @return 返回实体表中有哪些字段
     */
    @RequestMapping("/selectColumn")
    @ResponseBody
    public String selectColumn(MetaSensor metaSensor) {
        List<Map<String, String>> fields = new ArrayList<>();
        Map<String, String> field = new HashMap<>();
        field.put("field", "ID");
        field.put("title", "标示符");
        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "NAME");
//        field.put("title", "设备编号");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "SORT");
//        field.put("title", "设备类别");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "WORKSHOP");
//        field.put("title", "所属车间");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "OBJECT_NAME");
//        field.put("title", "测试对象名称");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "UNIT");
//        field.put("title", "单位");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "OPERATOR");
//        field.put("title", "录入人员编号");
//        fields.add(field);
        List<Column> columns = metaSensorService.selectColumn(metaSensor.getTechProcessId());
        for (Column column : columns) {
            field = new HashMap<>();
            //oracle 里查出来都是大写的尴尬了，与下面select相对应
//            field.put("field", column.enName.toUpperCase());
            field.put("field", column.getEnName());
            field.put("title", column.getCsName());
            field.put("dataType", column.getDataType());
            fields.add(field);
        }
        return JSON.toJSONString(fields);
    }

    /**
     * @return 返回实体表中有哪些字段
     */
    @RequestMapping("/selectRealColumn")
    @ResponseBody
    public String selectRealColumn(MetaSensor metaSensor) {

        int company_id = metaSensor.getCompany_id();
        List<Map<String, String>> fields = new ArrayList<>();

        Map<String, String> field = new HashMap<>();
        field.put("field", "ID");
        field.put("title", "标示符");
        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "NAME");
//        field.put("title", "设备编号");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "SORT");
//        field.put("title", "设备类别");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "WORKSHOP");
//        field.put("title", "所属车间");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "OBJECT_NAME");
//        field.put("title", "测试对象名称");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "UNIT");
//        field.put("title", "单位");
//        fields.add(field);
//
//        field = new HashMap<>();
//        field.put("field", "OPERATOR");
//        field.put("title", "录入人员编号");
//        fields.add(field);
        List<Column> columns = metaSensorService.selectRealColumn(company_id);
        for (Column column : columns) {
            field = new HashMap<>();
            field.put("field", column.enName.toUpperCase());
            field.put("title", column.csName);
            field.put("dataType", column.getDataType());
            fields.add(field);
        }
        return JSON.toJSONString(fields);
    }
    /**
     * @param list 前端传递过来的数据
     * @return 返回实体表中有哪些字段
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map<String, String> insert(@RequestBody List<Object> list) {
        return entitySensorService.insertEntitySensor(list);
    }

    /**
     * @return 成功返回1, 失败返回0
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestBody List<Integer> listId) {
        Map<String, Object> modelMap = new HashMap<>();
        if (entitySensorService.deleteEntitySensor(listId) == 1) {
            modelMap.put("status", "success");
        } else {
            modelMap.put("status", "failed");
        }
        return modelMap;
    }

    /**
     * @return 返回Map 如果查询出错，status为failed,msg为报错信息
     * 如果查询正确，status为success,如果只有一条数据,返回<result,object>,多条数据返回<results,object>
     */
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> select(MetaSensor metaSensor) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Map<String, Object>> entitySensorlist = entitySensorService.selectEntitySensor(metaSensor.getTechProcessId());
        modelMap.put("rows", entitySensorlist);
        return modelMap;
    }

    /**
     * @param list 所更新的数据
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> update(@RequestBody List<Object> list) {
        return entitySensorService.updateEntitySensor(list);
    }

    /**
     * @param listid 查询该设备是否被删除
     * @return
     */
    @RequestMapping("/queryEntityStatus")
    @ResponseBody
    public Map<String, String> queryEntityStatus(@RequestBody List<Integer> listid) {
        int id = listid.get(0);
        Map<String, String> modelMap = new HashMap<>();
        modelMap.put("status", "success");
        if (entitySensorService.queryEntityStatus(id) == 1) {
            modelMap.put("status", "success");
        } else {
            modelMap.put("status", "failed");
            modelMap.put("msg", "设备已删除，无法编辑");
        }
        return modelMap;
    }

    /**
     * 查询已有设备id
     * @return
     */
    @RequestMapping(value="/selectname",method=RequestMethod.GET)
    @ResponseBody
    public List<String> selectEntitySensorNameController(){
        return entitySensorService.selectEntitySensorName();
    }
}
